package xin.alum.aim.coder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import xin.alum.aim.model.Aim;
import xin.alum.aim.util.GZipUtil;

import java.util.List;

/**
 * Protobuf 协议转换
 *
 * @author Alum(alum @ live.cn)
 * @date 2021/8/5 17:05
 */
public class ProtoDecoder extends ProtobufDecoder {

    public ProtoDecoder() {
        super(Aim.Packet.getDefaultInstance());
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> list) {
        ChannelPipeline pipeline = ctx.channel().pipeline();
        try {
            in = GZipUtil.ungzip(xin.alum.aim.Aim.properties.getGzip(), in);
            super.decode(ctx, in, list);
        } catch (Exception ex) {
            pipeline.remove(this);
            ctx.fireChannelRead(in);
        }
    }
}
